package org.apache.ibatis.common.parsing;

/**
 * @Name GenericTokenParser
 * @Description: 通用占位符解析器
 * @Version v1.0.0
 * @Author: lichao
 * @Date: 2024-05-16 15:33:02
 **/
public class GenericTokenParser {
    // 占位符的起始标志 ['${', '#{']
    private final String openToken;
    // 占位符的结束标志 ['}']
    private final String closeToken;
    // 占位符处理器
    private final TokenHandler handler;

    public GenericTokenParser(String openToken, String closeToken, TokenHandler handler) {
        this.openToken = openToken;
        this.closeToken = closeToken;
        this.handler = handler;
    }

    /**
     * 占位符解析方法
     *
     * @param text 输入 SELECT * FROM t_action WHERE `id`=#{id} ORDER BY `actionTime`
     * @return java.lang.String 输出 SELECT * FROM t_action WHERE `id`=? ORDER BY `actionTime`
     * @version v1.0.0
     * @author lichao
     * @date 2024-05-16 15:35:40
     */
    public String parse(String text) {
        if (text == null || text.isEmpty()) return "";
        int start = text.indexOf(openToken);
        if (start == -1) return text;
        char[] src = text.toCharArray();
        int offset = 0;
        final StringBuffer buffer = new StringBuffer();
        StringBuffer expression = null;
        while (start > -1) {
            if (start > 0 && src[start - 1] == '\\') {
                // 占位符的起始标志逃逸了，删除反斜杠并继续
                buffer.append(src, offset, start - offset - 1).append(openToken);
                offset = start + openToken.length();
            } else {
                // 找到了占位符的起始标志，开始查找结束标志
                if (expression == null) expression = new StringBuffer();
                else expression.setLength(0);
                buffer.append(src, offset, start - offset);
                offset = start + openToken.length();
                int end = text.indexOf(closeToken, offset);
                while (end > -1) {
                    if (end > offset && src[end - 1] == '\\') {
                        // 占位符的结束标志逃逸了，删除反斜杠并继续
                        expression.append(src, offset, end - offset - 1).append(closeToken);
                        offset = end + closeToken.length();
                        end = text.indexOf(closeToken, offset);
                    } else {
                        // 找到了占位符的结束标志，获取结果
                        expression.append(src, offset, end - offset);
                        break;
                    }
                }
                if (end == -1) {
                    // 未找到占位符的结束标志
                    buffer.append(src, start, src.length - start);
                    offset = src.length;
                } else {
                    // 调用占位符处理器进行解析
                    buffer.append(handler.handleToken(expression.toString()));
                    offset = end + closeToken.length();
                }
            }
            start = text.indexOf(openToken, offset);
        }
        if (offset < src.length) buffer.append(src, offset, src.length - offset);
        return buffer.toString();
    }

}

